package cn.hh.controller;

import cn.hh.bean.ApiResultTO;
import cn.hh.bean.PageResult;
import cn.hh.bean.User;
import cn.hh.bean.dto.UserDTO;
import cn.hh.bean.enumbean.ResultCodeEnum;
import cn.hh.bean.user.UserTo;
import cn.hh.mapper.UserMapper;
import cn.hh.service.IUserService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.org.apache.xpath.internal.operations.Plus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author huangh
 * @date 2021/09/28
 * @Description:
 */
@RestController
@RequestMapping()
public class UserController {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private IUserService userService;

    //get请求碰到问题
    //https://blog.csdn.net/hd20086996/article/details/114891112
    //http://localhost:8080/getUser?type=1&other=2
    @GetMapping("/getUser")
    public ApiResultTO<UserTo>  getUser(@RequestParam("type") String type,@RequestParam("other") String other){
        System.out.println("type:" + type +",other:"+other);
        if(StrUtil.isEmpty(type)) {
            // 也可以把常见的错误信息抽取到ErrorCodeEnum并传入（参考小册《枚举的应用》）
            return ApiResultTO.buildFailed(ResultCodeEnum.FAILURE,"类型不存在");
        }
        if("1".equals(type)){
            return ApiResultTO.buildFailed(ResultCodeEnum.FAILURE);
        }else if("2".equals(type)){
            UserTo data = new UserTo();
            data.setAge(11);
            data.setName("辉哥牛逼");
            return ApiResultTO.buildSuccess(data);
        }

        return null;
    }

    //分页必须在 MybatisPlusConfig 配置
    @PostMapping("/listUser")
    public PageResult<User> listUser(@RequestBody UserDTO userDTO) {

        // 查询并返回（userService返回的是Page对象，含有列表+分页信息）
        Page<User> page = new Page<User>(userDTO.getPage(), userDTO.getSize());
        userMapper.selectPage(page,null);
        //userService.listPage(page, userDTO);
        return PageResult.success(page);
    }


    //ApiResultTO PageResult 统一结果封装
    /*
    ● 客户端/前端传入的DTO
    ● 与数据库字段映射的DO
    ● 返回给客户端/前端的VO  TO
    */
    /*
    MyBatis-Plus 分页查询以及自定义sql分页
    https://blog.csdn.net/weixin_38111957/article/details/91554108
        */
}
